package mkis.service.root.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import mkis.service.root.data.dao.MemberDAO;
import mkis.service.root.data.dao.TestDAO;
import mkis.service.root.data.model.JsonTemplete;
import mkis.service.root.data.model.Member;
import mkis.service.root.data.model.OrgParam;
import mkis.service.root.service.MemberService;

@Service("memberService")
public class MemberServiceImpl implements MemberService {
	
	@Resource(name="memberDAO")
	private MemberDAO memberDAO;
	
	
	@Override
	public Member getMember(Map<String, Object> queryParam) throws Exception {
		return memberDAO.getMember(queryParam);
	}
	
	@Override
	public Member getLoginMember(Map<String, Object> queryParam)
			throws Exception {
		return memberDAO.getLoginMember(queryParam);
	}
	
	
	@Override
	public Map<String,Object> getMemberListAndCnt(Map<String, Object> paramMap) throws Exception {
		Map<String,Object> resultMap=new HashMap<String, Object>();
		int cnt=memberDAO.getMemberListCnt(paramMap);
		List<Member> list=memberDAO.getMemberList(paramMap);
		
		resultMap.put("totalcnt", cnt);
		resultMap.put("list", list);
		
		return resultMap;
	}
	
	@Override
	public int updateMember(Member member) throws Exception {
		return memberDAO.updateMember(member);
	}
	
	@Override
	public int outMember(Map<String, Object> queryParam) throws Exception {
		return memberDAO.outMember(queryParam);
	}
	
	@Override
	public int insertMember(Member member) throws Exception {
		return memberDAO.insertMember(member);
	}
	
	@Override
	public int checkOrgList(List<String> paramList) throws Exception {
		return memberDAO.checkOrgList(paramList);
	}
	
	public JsonTemplete checkMemberParamName(OrgParam orgParam,JsonTemplete jsonTemplete, String resultCode, String resultName) throws Exception {
		List<String> paramList=new ArrayList<String>();
	    
	    int paramCnt1=0;
	    String answer11=orgParam.getAnswer11();
	    String answer12=orgParam.getAnswer12();
	    String answer13=orgParam.getAnswer13();
	    String answer14=orgParam.getAnswer14();
	    
	    int paramCnt2=0;
	    String answer21=orgParam.getAnswer21();
	    String answer22=orgParam.getAnswer22();
	    String answer23=orgParam.getAnswer23();
	    String answer24=orgParam.getAnswer24();
	    
	    if(answer11!=null && !"".equals(answer11)){
	    	paramList.add(answer11);
	    	paramCnt1++;
	    }
	    if(answer12!=null && !"".equals(answer12)){
	    	paramList.add(answer12);
	    	paramCnt1++;
	    }
	    if(answer13!=null && !"".equals(answer13)){
	    	paramList.add(answer13);
	    	paramCnt1++;
	    }
		if(answer14!=null && !"".equals(answer14)){
			paramList.add(answer14);
			paramCnt1++;
		}
		
	  	
		
		int resultCnt1=checkOrgList(paramList);
		
		paramList.clear();
		
		if(answer21!=null && !"".equals(answer21)){
			paramList.add(answer21);
			paramCnt2++;
	    }
	    if(answer22!=null && !"".equals(answer22)){
	    	paramList.add(answer22);
	    	paramCnt2++;
	    }
	    if(answer23!=null && !"".equals(answer23)){
	    	paramList.add(answer23);
	    	paramCnt2++;
	    }
		if(answer24!=null && !"".equals(answer24)){
			paramList.add(answer24);
			paramCnt2++;
		}
		
		
		
		
		int resultCnt2=checkOrgList(paramList);
		
		if(paramCnt1==0) {
	    	resultCode="0000";
	    	resultName="정상";
	    } 
		
		if(paramCnt2==0) {
	    	resultCode="0000";
	    	resultName="정상";
	    } 
		
		
		if(paramCnt1==0 && paramCnt2==0) {
			jsonTemplete.setResultCode(resultCode);
		    jsonTemplete.setResultName(resultName);
		    return jsonTemplete;
		    
		} else if(resultCnt1==paramCnt1 && resultCnt2==paramCnt2) {
	    	resultCode="0000";
	    	resultName="정상";
	    } else {
	    	if(resultCnt1!=paramCnt1) {
	    		resultCode="0001";
		    	resultName="[1번 항목]\n동료 이름을 확인바랍니다.";
	    	} else if(resultCnt2!=paramCnt2) {
    			resultCode="0002";
    			resultName="[2번 항목]\n동료 이름을 확인바랍니다.";
	    	}
	    }
	    
	    jsonTemplete.setResultCode(resultCode);
	    jsonTemplete.setResultName(resultName);
	    
	    return jsonTemplete;
	}
}
